package cn.hrlly.tao2shou.callback;

import com.google.gson.Gson;
import com.zhy.http.okhttp.callback.Callback;

import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.domain.ApplicationList;
import cn.hrlly.tao2shou.listener.OnApplicationListListener;
import okhttp3.Request;
import okhttp3.Response;

/**
 * User: gengliang
 * Date: 2016-01-31
 * Time: 10:51
 */

public class ApplicationListCallback extends Callback<ApplicationList> {

    private OnApplicationListListener onApplicationListListener;

    public ApplicationListCallback(OnApplicationListListener onApplicationListListener){
        this.onApplicationListListener=onApplicationListListener;
    }

    @Override
    public ApplicationList parseNetworkResponse(Response response) throws Exception {
        String string=response.body().string();
        ApplicationList applicationList=new Gson().fromJson(string,ApplicationList.class);
        return applicationList;
    }

    @Override
    public void onError(Request request, Exception e) {
        onApplicationListListener.showApplicationListFail(R.string.network_connection_error);
    }

    @Override
    public void onResponse(ApplicationList applicationList) {
        if(applicationList.getStatus()==0)
            onApplicationListListener.showApplicationListSucc(applicationList);
        else
            onApplicationListListener.showApplicationListFail(applicationList.getErrorMessage());
    }
}
